<template>
<div>
  <el-form :model="user" :rules="rules" ref="user">
    <el-row>
      <el-col :span="8">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" />
        </el-form-item>
      </el-col>
      <el-col :span="9" :offset="2">
        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="地址" prop="address">
          <AreaData @change="test" width="100%"></AreaData>
        </el-form-item>
      </el-col>
      <el-col :span="5"  :offset="2">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="user.phone"/>
        </el-form-item>
      </el-col>
      <el-col :span="3" :offset="1">
        <el-form-item label="权限">
          <el-select v-model="user.role">
            <el-option label="管理员" :value="0" />
            <el-option label="普通用户" :value="1" />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="3" :offset="8">
        <el-button type="primary" @click="add">添加用户</el-button>
      </el-col>
    </el-row>
  </el-form>
</div>
</template>

<script>

import {addUser} from "@/api/user";
import AreaData from "@/components/AreaData";

export default {
  name: "UserAdd",
  components: {AreaData},
  data() {
    return {
      user:{
        username:'',
        password:'',
        phone:'',
        address:'',
        role:1
      },
      rules:{
        username:[
          {required:true,message:'请输入用户名',trigger:'blur'},
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password:[
          {required:true,message:'请输入密码',trigger:'blur'},
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        phone:[
          {required:true,message:'请输入手机号',trigger:'blur'},
          { max: 11 , message: '长度不超过 11 个字符', trigger: 'blur'}
        ],
        address:[
          {required:true}
        ]
      }
    }
  },
  methods:{
    add(){
      addUser(this.user).then((data) => {
        if (data.code == 200) {
          this.$message.success(data.msg)
        }else{
          this.$message.error("添加失败")
        }
      })
      },
    test(e){
      this.user.address = e
    }
    }
}
</script>

<style scoped>

</style>
